const router = require('express').Router()
const {query} = require('../../db')

router.get('/', async (req, res, next)=>{
    let page = req.query.page || 1
    let pageSize = req.query.page_size || 20

    let offset = (page-1)*pageSize
    let sql = `SELECT * FROM categories LIMIT ${offset},${pageSize}`

    let data = await query(sql).catch(next)
    if(data === undefined) return

    let total = await query('SELECT COUNT(*) total FROM categories').catch(next)
    if(total === undefined) return

    res.json({
        'ok': 1,
        'total': total[0].total,
        data
    })
})


module.exports = router